package com.treasure.service.collocate.service.impl;

import com.treasure.service.collocate.service.GainStatisticsService;
import com.treasure.service.commons.lang.util.BeanUtil;
import com.treasure.service.commons.lang.util.IdCreater;
import com.treasure.service.commonutil.Iterables;
import com.treasure.service.domain.TGainTtatistics;
import com.treasure.service.dto.RankingListDto;
import com.treasure.service.mapper.TGainTtatisticsMapper;
import com.treasure.service.third.utils.TokenConfig;
import com.treasure.service.third.utils.UserToken;
import com.treasure.service.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @Description:    排行榜统计
* @Author:         panxinwen
* @CreateDate:     2018/12/9 12:27
* @Version:        1.0
*/
@Service
public class GainStatisticsServiceImpl implements GainStatisticsService {

    @Autowired
    private TGainTtatisticsMapper tGainTtatisticsMapper;

    @Override
    public List<RankingListDto> getRankingList(Long userId,Integer timeType,Integer dataType) {
        Long timeStamp = DateUtil.getTimestampType(timeType);
        List<TGainTtatistics> list = tGainTtatisticsMapper.getRankingList(timeStamp,Long.parseLong(DateUtil.getNowTime(DateUtil.DATE_SMALL_DD)),dataType);

        List<RankingListDto> result = new ArrayList<>();

//        list.forEach(tGainTtatistics ->{
//
//            RankingListDto rankingListDto = BeanUtil.copy(tGainTtatistics,RankingListDto::new);
//            UserToken userToken = TokenConfig.getToken(tGainTtatistics.getUserId());
//            rankingListDto.setHeadimgurl(userToken.getHeadimgurl());
//            rankingListDto.setNikeName(userToken.getNickname());
//            rankingListDto.setRanking(index);
//            result.add(rankingListDto);
//            index++;
//
//        });

        Iterables.forEach(list, (index, tGainTtatistics) -> {
            RankingListDto rankingListDto = BeanUtil.copy(tGainTtatistics,RankingListDto::new);
            UserToken userToken = TokenConfig.getToken(tGainTtatistics.getUserId());
            rankingListDto.setHeadimgurl(userToken.getHeadimgurl());
            rankingListDto.setNikeName(userToken.getNickname());
            rankingListDto.setRanking(Long.parseLong(index+""));
            result.add(rankingListDto);
            }
        );

        return result;
    }

    @Override
    public void calculatePrice(Long userId,Integer type, Long price) {

        TGainTtatistics tGainTtatistics = new TGainTtatistics();
        tGainTtatistics.setThatDay(Long.parseLong(DateUtil.getNowTime(DateUtil.DATE_SMALL_DD)));
        tGainTtatistics = tGainTtatisticsMapper.selectOne(tGainTtatistics);
        if (tGainTtatistics ==null || tGainTtatistics.getId() == null){
            tGainTtatistics = new TGainTtatistics();
            tGainTtatistics.setThatDay(Long.parseLong(DateUtil.getNowTime(DateUtil.DATE_SMALL_DD)));
            tGainTtatistics.setAmount(0l);
            tGainTtatistics.setApplyCardGain(0l);
            tGainTtatistics.setCreateTime(new Date());
            tGainTtatistics.setId(IdCreater.getLongId());
            tGainTtatistics.setInviteCardGain(0l);
            tGainTtatistics.setInviteVipGain(0l);
            tGainTtatistics.setShareCardGain(0l);
            tGainTtatistics.setTeamGain(0l);
            tGainTtatistics.setUserId(userId);
            tGainTtatistics.setUpdateTime(new Date());
            tGainTtatistics = calculate(type,tGainTtatistics,price);
            tGainTtatisticsMapper.insert(tGainTtatistics);
        }else {
            tGainTtatistics = calculate(type,tGainTtatistics,price);
            tGainTtatisticsMapper.updateByPrimaryKeySelective(tGainTtatistics);
        }

    }

    @Override
    public RankingListDto getMyRanking(Long userId, Integer timeType, Integer dataType) {
        Long timeStamp = DateUtil.getTimestampType(timeType);
        TGainTtatistics tGainTtatistics =tGainTtatisticsMapper.getMyStatistics(userId,timeStamp,Long.parseLong(DateUtil.getNowTime(DateUtil.DATE_SMALL_DD)));
        RankingListDto rankingListDto = BeanUtil.copy(tGainTtatistics,RankingListDto::new);
        UserToken userToken = TokenConfig.getToken(userId);
        rankingListDto.setHeadimgurl(userToken.getHeadimgurl());
        rankingListDto.setNikeName(userToken.getNickname());
        Long ranking = tGainTtatisticsMapper.getMyRanking(rankingListDto.getAmount(),timeStamp,Long.parseLong(DateUtil.getNowTime(DateUtil.DATE_SMALL_DD)));
        rankingListDto.setRanking(ranking+1);
        return rankingListDto;
    }

    private TGainTtatistics calculate(int type,TGainTtatistics tGainTtatistics,Long price){

        switch (type){

            case 1:
                tGainTtatistics.setApplyCardGain(tGainTtatistics.getApplyCardGain()+price);
                break;
            case 2:
                tGainTtatistics.setInviteCardGain(tGainTtatistics.getInviteCardGain()+price);
                break;
            case 3:
                tGainTtatistics.setInviteVipGain(tGainTtatistics.getInviteVipGain()+price);
                break;
            case 4:
                tGainTtatistics.setShareCardGain(tGainTtatistics.getShareCardGain()+price);
                break;
            case 5:
                tGainTtatistics.setTeamGain(tGainTtatistics.getTeamGain()+price);
                break;
            default:
                break;
        }
        tGainTtatistics.setAmount(tGainTtatistics.getAmount()+price);

        return tGainTtatistics;

    }

}
